<?php
/**
 * Installer initial site configuration page
 *
 * @copyright		The XOOPS project http://www.xoops.org/
 * @license			http://www.fsf.org/copyleft/gpl.html GNU General Public License (GPL)
 * @package			installer
 * @since			2.0.18
 * @version			$Id: page_siteinit.php 596 2006-06-30 03:14:03Z skalpa $
 *
 * @author			Haruki Setoyama  <haruki@planewave.org>
 * @author			Kazumi Ono <webmaster@myweb.ne.jp>
 * @author			Skalpa Keo <skalpa@xoops.org>
 * @author			dugris <dugris@frxoops.org>
**/

require_once 'common.inc.php';
if ( !defined('XOOPS_INSTALL') ) { die('XOOPS Custom Installation die'); }

$wizard->setPage( 'siteinit' );
$pageHasForm = true;
$pageHasHelp = false;

$vars =& $_SESSION['siteconfig'];

$error =& $_SESSION['error'];
$iswebmaster = 0;

if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
	$vars['adminname'] = $_REQUEST['adminname'];
	$vars['adminmail'] = $_REQUEST['adminmail'];
	$vars['adminpass'] = $_REQUEST['adminpass'];
	$vars['adminpass2'] = $_REQUEST['adminpass2'];
	$error = '';

	if (!preg_match( "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+([\.][a-z0-9-]+)+$/i", $vars['adminmail'] ) ) {
		$error = ERR_INVALID_EMAIL;
	} elseif ( @empty( $vars['adminname'] ) || @empty( $vars['adminpass'] ) || @empty( $vars['adminmail'] ) ) {
		$error = ERR_REQUIRED;
	} elseif ( $vars['adminpass'] != $vars['adminpass2'] ) {
		$error = ERR_PASSWORD_MATCH;
	}

   if ( $error ) {
   	$wizard->redirectToPage( '+0' );
   	return 200;
   } else {
   	$wizard->redirectToPage( '+1' );
   	return 302;
   }
} else {
	include_once './class/dbmanager.php';
	$dbm =& new db_manager();

	if ( !$dbm->isConnectable() ) {
		$wizard->redirectToPage( 'dbsettings' );
		exit();
	}

	$res = $dbm->query( "SELECT COUNT(*) FROM " . $dbm->db->prefix( "users" ) );
	list ( $iswebmaster ) = $dbm->db->fetchRow( $res );
}

ob_start();

echo '<script type="text/javascript">
		var desc = new Array();
		desc[0] = "' . PASSWORD_VERY_WEAK . '";
		desc[1] = "' . PASSWORD_WEAK . '";
		desc[2] = "' . PASSWORD_BETTER . '";
		desc[3] = "' . PASSWORD_MEDIUM . '";
		desc[4] = "' . PASSWORD_STRONG . '";
		desc[5] = "' . PASSWORD_STRONGEST . '";
		</script>';


if ( !empty( $error ) ) {
	echo "<div class='x2-note errorMsg'>" . $error . "</div>\n";
}
if ( $iswebmaster ) {
	$pageHasForm = false;
	echo "<div class='x2-note errorMsg'>" . WEBMASTER_EXIST . "</div>\n";
} else {
	echo "<fieldset><legend>" . LEGEND_ADMIN_ACCOUNT . "</legend>";

	echo xoFormField( 'adminname',	$vars['adminname'], ADMIN_LOGIN_LABEL );
	echo xoFormField( 'adminmail',	$vars['adminmail'], ADMIN_EMAIL_LABEL );
	echo xoPassField( 'adminpass',	'', ADMIN_PASS_LABEL );
	echo xoPassField( 'adminpass2',	'', ADMIN_CONFIRMPASS_LABEL );

	echo "<hr>";
	echo "<label for='passwordStrength'>" . PASSWORD_LABEL . "</label><div id='passwordStrength' class='strength0'><span id='passwordDescription'>" . PASSWORD_DESC . "</span></div>";

	echo "<hr>";
	echo "<label for='password_generator'>" . PASSWORD_GENERATOR . "</label>";
	echo "<span>";
	echo "<input type='text' name='generated_pw' id='generated_pw' value='' />";
	echo "<button type='button' onclick='javascript:suggestPassword(14);'/>" . PASSWORD_GENERATE ."</button>";
	echo "<button type='button' onclick='javascript:suggestPasswordCopy(\"adminpass\");'/>" . PASSWORD_COPY . "</button>";
	echo "</span>";

	echo "</fieldset>";
}
$content = ob_get_contents();
ob_end_clean();
$error = '';

include 'install_tpl.php';

?>